package com.abf.okhttp.pool;

import okhttp3.OkHttpClient;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @author: LCG
 * @date: 2022-05-24 11:08:22
 * @description: 池的构建
 **/
@Configuration
public class HttpClientPoolConfig {

    private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;

    @PostConstruct
    public void createAndConfigPool(){

        //创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        //设置最大连接数
        cm.setMaxTotal(100);

        //设置每个主机的最大连接数
        cm.setDefaultMaxPerRoute(10);

        this.poolingHttpClientConnectionManager=cm;

  /*      ConnectionConfig connectionConfig=new ConnectionConfig();
        HttpPost httpPost=new HttpPost();
        cm.setConnectionConfig();*/

    }

    /** 
     * @author: LCG
     * @date: 2022-05-24 11:21:07
     * @description:  获取HttpClient客户端
     */
    public HttpClient getOkHttpClient(){

        CloseableHttpClient build = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();

        return build;
    }

}
